//
//  DistubuteVC.m
//  u76ho
//
//  Created by m-air-01 on 15/12/29.
//  Copyright © 2015年 LV. All rights reserved.
//

#import "DistubuteVC.h"
#import "DistubiteBtn.h"
#import "SendPictureViewController.h"
#import "CreateResumeVC.h"
#import "LoginViewController.h"
#import "MyPictureVC.h"

#import "ViewController.h"
@interface DistubuteVC ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate>
{
    UIImagePickerController * _pickerVC;
    int istakePhoto;
}
@end
static int Distubstate = 0;
static int isclose = 1;
@implementation DistubuteVC
#pragma mark - 视图生命周期
-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];

  
    isclose = 1;
   // [app.viewController isHiddenCustomTabBarByBoolean:YES];
    self.navigationController.navigationBarHidden = YES;
    NSLog(@"********************%i",Distubstate);
    if (Distubstate  == 0) {
        if (istakePhoto == 1) {
            istakePhoto = 0;
        }
        else
        {
//            [self main];
        }
    }
    else
    {
        Distubstate = 0;
    }
    
}
-(void)viewDidDisappear:(BOOL)animated
{

    
    [super viewDidDisappear:YES];
    if (isclose == 0) {
        
    }
    else
    {
        Distubstate = 1;
    }
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
//   self.view.backgroundColor = [UIColor redColor];
//
//    self.view.alpha = 0.9;
//    
   // [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Good"]]];
    
    [self main];
    
    
}
-(void)main
{
    istakePhoto = 0;
    UIImageView * bgImgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgImgV.userInteractionEnabled = YES;
    bgImgV.tag = 10;
  //  UIImage * image = [UIImage imageWithData:[NSUserDefaults objectForKey:@"blu"]];
    //bgImgV.image = image.blurImage();
    [self.view addSubview:bgImgV];
    
    
    //四个按钮
    NSArray * picArr = @[@[@"创建简历",@"发文"],@[@"创建图片库",@"发图"]];
    for (int i = 0; i<4; i++) {
        int m = i/2;//行号
        int n = i%2;//列号
        DistubiteBtn * btn = [DistubiteBtn buttonWithType:UIButtonTypeCustom];
        btn.imageView.contentMode = UIViewContentModeCenter;
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        btn.frame = CGRectMake(SIZETO6W(73) + m*(SCREEN_WIDTH-SIZETO6W(73)*2-90), SCREEN_HEIGHT-SIZETO6PlusH(60+53+30)-130*2 + n*120+SIZETO6H(30), 90, 130);
        [btn setImage:[UIImage imageNamed:picArr[m][n]] forState:UIControlStateNormal];
        [btn setTitle:picArr[m][n] forState:UIControlStateNormal];
        [btn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
        btn.tag = i+1;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [bgImgV addSubview:btn];
    }
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-SIZETO6H(60), SCREEN_WIDTH, SIZETO6H(60))];
    view.userInteractionEnabled = YES;
    view.backgroundColor = UIColorFromRGB(0xffffff);
    [bgImgV addSubview:view];
    
    UIButton * closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.frame = CGRectMake(SCREEN_WIDTH/2-32/2, view.height/2-32/2, 32, 32);
    
    //closeBtn.frame = CGRectMake(100, 60, 30, 30);
    [closeBtn setImage:[UIImage imageNamed:@"关闭图标"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:closeBtn];
    [UIView animateWithDuration:0.3 animations:^{
        bgImgV.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }];
}
////创建相册
//-(void)createAlumWithname:(NSString *)name
//{
//    __weak typeof(self)weakSelf = self;
//    [U76HONet createAlumWithaction:@"" code:[Singleton share].code name:name andDataBlock:^(NSDictionary *dic) {
//        NSLog(@"创建相册成功了");
//        [weakSelf performSelector:@selector(alertV) withObject:nil afterDelay:1.0];
//    }];
//}

-(void)removeFromView
{
    


    UIImageView * img = (UIImageView *)[self.view viewWithTag:10];
    [img removeFromSuperview];
}

-(void)closeAction:(id)sender
{
    
    
        AppDelegate * app = [UIApplication sharedApplication].delegate;
    
    app.viewController.tabBar.hidden = NO;
    
    
    [self dismissViewControllerAnimated:YES completion:nil];

    //[self performSelector:@selector(removeFromView) withObject:nil afterDelay:0.2];
}

#pragma mark --点击事件
-(void)btnAction:(UIButton *)btn
{
    
    Distubstate = 1;
    if ([self isLogin]) {
        int tag = (int)btn.tag;
        if (tag == 1) {
            NSLog(@"111");
            CreateResumeVC * createResumeVC = [[CreateResumeVC alloc]init];
            
            UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:createResumeVC];
            [createResumeVC setCreateBlock:^{
                
            }];
            [self presentViewController:nav animated:YES completion:nil];
            //[self.navigationController pushViewController:createResumeVC animated:YES];
            
        }
        else if (tag == 2)
        {
            NSLog(@"222");
            SendPictureViewController * sendPictureVC = [[SendPictureViewController alloc]init];
            
            UINavigationController * SendNav = [[UINavigationController alloc]initWithRootViewController:sendPictureVC];
            
            [self presentViewController:SendNav  animated:YES completion:nil];
            //[self.navigationController pushViewController:sendPictureVC animated:YES];
        }
        else if (tag == 3)
        {
            NSLog(@"333");
            Distubstate = 1;
//            [Singleton alercreateAlumeMessage:@"新建图集" content:@"请为此相册输入名称" btnName:@"确定"];
            MyPictureVC * mypicVC = [[MyPictureVC alloc]init];
            
            UINavigationController * MyPNAV  = [[UINavigationController alloc]initWithRootViewController:mypicVC];
            mypicVC.isCreate = YES;
            [self presentViewController:MyPNAV animated:YES completion:nil];
        }
        else
        {
            NSLog(@"444");
            Distubstate = 0;
            UIActionSheet * sheetsssss = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册", nil];
            
            
            [sheetsssss showInView:self.view];
        }
    }
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        Distubstate = 1;
        _pickerVC = [[UIImagePickerController alloc]init];
        [_pickerVC setSourceType:UIImagePickerControllerSourceTypeCamera];
        _pickerVC.delegate = self;
        [self presentViewController:_pickerVC animated:YES completion:nil];
    }
    else if(buttonIndex == 1)
    {
        Distubstate = 1;
        SendPictureViewController * sendPictureVC = [[SendPictureViewController alloc]init];
        UINavigationController * NAV = [[UINavigationController alloc]initWithRootViewController:sendPictureVC];
        sendPictureVC.sendPicture = YES;
        [self presentViewController:NAV animated:YES completion:nil];
    }
}
//将选中的图片付给头像
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
     
    
    UIImage * selectImg = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:nil];
    istakePhoto = 1;
    
    SendPictureViewController * sendpicVC = [[SendPictureViewController alloc]init];
    sendpicVC.img = selectImg;
    
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sendpicVC ];

    
    
    [self presentViewController:nav animated:YES completion:nil];
    
}

-(BOOL)isLogin
{
    
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {

        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            
            [self showViewController:alerVC sender:nil];
            
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                
                UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
                
                nav.navigationBarHidden = YES;
                [self presentViewController:nav animated:YES completion:nil];
                
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            
            //[self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        [self presentViewController:nvc animated:YES completion:nil];
    }
}


-(void)alertV
{
    [Singleton alerSendMessage:@"创建成功"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
